import { login } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
const state = {
  token: getToken()
}
const mutations = {
  // 存储 token
  setToken(state, data) {
    // 数据持久化，存在 cookie 中
    setToken(data)
    state.token = data
  },
  // 移除token
  removeToken(state) {
    state.token = null
    localStorage.removeItem('token')
  }
}
const actions = {
  async login(store, data) {
    // 发请求
    const res = await login(data)
    // console.log(res)
    // 拿 token
    const token = res.data.data
    // 存 token
    store.commit('setToken', token)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
